package com.visionork.android;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.visionork.rss.FeedParser;
import com.visionork.rss.FeedParserFactory;
import com.visionork.rss.Message;
import com.visionork.rss.ParserType;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class VarietyShowsActivity extends ListActivity {
	private List<Message> messages;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		loadFeed();
		/*
		 * Log.i("AndroidNews", "hello"); setListAdapter(new
		 * ArrayAdapter<String>(this, R.layout.list_item, SHOWLIST));
		 * 
		 * ListView lv = getListView(); lv.setTextFilterEnabled(true);
		 * 
		 * 
		 * lv.setOnItemClickListener(new OnItemClickListener() { public void
		 * onItemClick(AdapterView<?> parent, View view, int position, long id)
		 * { // When clicked, show a toast with the TextView text
		 * Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
		 * Toast.LENGTH_SHORT).show(); } });
		 */
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		@SuppressWarnings("unused")
		String url = messages.get(position).getLink().toExternalForm();
		String youtubeID = getYoutubeID(url);
		playYoutubeVideo(youtubeID);
	}

	private void loadFeed() {
		FeedParser parser = FeedParserFactory.getParser(ParserType.TW);
		messages = parser.parse();
		List<String> titles = new ArrayList<String>(messages.size());
		for (Message msg : messages) {
			titles.add(msg.getTitle());
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.layout.row, titles);
		this.setListAdapter(adapter);
	}

	private void playYoutubeVideo(String videoID) {
		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + videoID)); 
		startActivity(i);
	}
	
	private String getYoutubeID(String urlString)
	{	
		String id = null;
		URL url = null;
		try {
			url = new URL(urlString);
		} catch (MalformedURLException e) { // TODO Auto-generated catch block
			e.printStackTrace();
		}
		InputStream ins = null;
		try {
			ins = url.openStream();
		} catch (IOException e) { // TODO Auto-generated
			e.printStackTrace();
		}

		Scanner s = new Scanner(ins);
		while (s.hasNextLine()) {
			String line = s.nextLine();
			// <param name="movie"
			// value="http://www.youtube.com/v/jzxLoKSd_qQ?version=3&autoplay=1&showinfo=0&iv_load_policy=3&cc_load_policy=1&rel=0">
			Pattern r = Pattern.compile("http://www.youtube.com/v/([^\\?]*)\\?");
			Matcher m = r.matcher(line);
			if (m.find()) {
				id = m.group(1);
				break;
			}
		}
		s.close();
		
		return id;
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add("Refresh");
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		Toast.makeText(getApplicationContext(), "Refresh", Toast.LENGTH_SHORT)
				.show();
		return true;
	}

	static final String[] SHOWLIST = new String[] { "康熙来了", "王牌大间谍" };
}
